package com.xhwl.centiotdata.utils;

import com.xhwl.centiotdata.service.cent.IAccountCentService;
import com.xhwl.centiotdata.service.cent.IProjectCentService;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import com.xhwl.common.query.cent.sdata.ProjectQuery;
import com.xhwl.common.query.cent.sdata.StaffQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: lixy
 * @CreateDate: 2023/2/10 13:50
 */
@Component
@Slf4j
public class AccountUtil {

    @Resource
    private IAccountCentService iAccountCentService;

    @Resource
    private IProjectCentService projectCentService;

    public StaffDTO handleAccount(AccountDTO accountDTO) {
        StaffDTO staffDTO = new StaffDTO();
        staffDTO.setAccountId(accountDTO.getId());
        staffDTO.setName(accountDTO.getName());
        staffDTO.setEmail(accountDTO.getEmail());
        staffDTO.setSex(accountDTO.getSex());
        staffDTO.setPhone(accountDTO.getPhone());
        staffDTO.setCardNo(accountDTO.getIdCard());
        staffDTO.setIdCard(accountDTO.getIdCard());
        staffDTO.setFaceId(accountDTO.getFaceId());
        staffDTO.setFaceUrl(accountDTO.getFaceUrl());

        StaffDTO staffOne = getStaffOne(accountDTO.getEnterpriseId(), accountDTO.getId());
        if (!Objects.isNull(staffOne) && !CollectionUtils.isEmpty(staffOne.getOrgIds())) {
            staffDTO.setOrgIds(staffOne.getOrgIds());
        }
        return staffDTO;
    }

    public StaffDTO getStaffOne(Integer enterpriseId, Integer accountId) {
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setEnterpriseId(enterpriseId);
        staffQuery.setAccountId(accountId);
        StaffDTO staffOne = iAccountCentService.one(staffQuery);
        if (Objects.isNull(staffOne) || staffOne.getId() == null) {
            try {
                Thread.sleep(1500);
                staffOne = iAccountCentService.one(staffQuery);
                log.info("staffOne{}", staffOne);
            } catch (InterruptedException e) {
                log.error(e.getMessage());
                return null;
            }
        }
        return staffOne;
    }

    public List<Integer> getProjectIds(AccountDTO accountDTO) {
        List<Integer> projectIds = accountDTO.getProjectIds();
        if (CollectionUtils.isEmpty(projectIds) && CollectionUtils.isNotEmpty(accountDTO.getEnterpriseIds())) {
            ProjectQuery projectQuery = new ProjectQuery();
            projectQuery.setEnterpriseIds(accountDTO.getEnterpriseIds());
            projectIds = projectCentService.list(projectQuery).stream().map(Project::getId).distinct().collect(Collectors.toList());
        }
        return projectIds;
    }
}
